import pygame.font
from .eventCenter import EventCenter

class Button(EventCenter):

    def __init__(self, screen, msg):

        pygame.font.init()
        self.screen = screen
        self.msg = msg
        self.screen_rect = self.screen.get_rect()
        
        self.width = 200
        self.height = 50
        self.bg_color = (0, 255, 0)
        self.text_color = (255, 255, 255)
        self.font = pygame.font.SysFont(None, 48)


        self.rect = pygame.Rect(0, 0, self.width, self.height)
        self.rect.center = self.screen_rect.center

        self.propMsg(msg)


    def propMsg(self, msg):
        
        self.msg_img = self.font.render(msg, True, self.text_color, self.bg_color)
        self.msg_img_rect = self.msg_img.get_rect()
        self.msg_img_rect.center = self.rect.center


    def drawButton(self):
        self.screen.fill(self.bg_color, self.rect)
        self.screen.blit(self.msg_img, self.msg_img_rect)

    def bindMouseEvent(self, fn):
        self.mouseEventFn = self.mouseEvent(fn)
        self.registered(pygame.MOUSEBUTTONDOWN, self.mouseEventFn)
    
    def mouseEvent(self, fn):
        
        def callback(event):
            x, y = pygame.mouse.get_pos()
            if self.rect.collidepoint(x, y):
                fn()

        return callback
